using System;
using SubversionReports.Entities;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class RevisionsView : PagedControl
    {
        private Repository repository;

        /// <summary>
        /// Gets the repository.
        /// </summary>
        /// <value>The repository.</value>
        protected Repository Repository
        {
            get
            {
                if(repository == null)
                    repository = SvnWebDav.GetRepositoryLog(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword);
                return repository;
            }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(SvnWebDav.AuthenticationSuceeded && Visible)
            {
                Repository filteredRepository = Repository;
                if (!string.IsNullOrEmpty(Request["Filters"]))
                {
                    foreach (string filter in Request["Filters"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        string[] filterParams = filter.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        if (filterParams.Length.Equals(3))
                            filteredRepository = filteredRepository.Filter(filterParams[0], GetFilterObject(filterParams[0], filterParams[1]), filterParams[2].Equals("Exclude") ? FilterType.Exclude : FilterType.Include);
                        else if (filterParams.Length.Equals(4))
                            filteredRepository = filteredRepository.Filter(filterParams[0], GetFilterObject(filterParams[0], filterParams[1]), GetFilterObject(filterParams[0], filterParams[2]), filterParams[3].Equals("Exclude") ? FilterType.Exclude : FilterType.Include);
                    }
                }
                RepositoryTitle.Text = filteredRepository.Url;
                RepositoryDescription.Text = filteredRepository.Description;

                RevisionsGridView.DataSource = filteredRepository.GetPage(PageNumber, PageSize, "Id", Entities.SortDirection.Descending);
                RevisionsGridView.DataBind();
                PageLinks1.NonPagedCount = filteredRepository.NonPagedCount;
                PageLinks2.NonPagedCount = filteredRepository.NonPagedCount;
            }
        }

        private static object GetFilterObject(string property, string value)
        {
            switch (property)
            {
                case "Author":
                    return value;
                case "Id":
                    return int.Parse(value);
                case "Date":
                    return DateTime.Parse(value);
                default:
                    throw new ArgumentOutOfRangeException("property", property, "property must be one of: Id, Author, Date.");

            }
        }

        /// <summary>
        /// Gets the author icon URL.
        /// </summary>
        /// <param name="author">The author.</param>
        /// <returns></returns>
        protected string GetAuthorIconUrl(string author)
        {
            return Repository.AuthorIcons[author];
        }
    }
}